package com.fairylove.test;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class ZookeeperTestClient {
    private static final int BASE_SLEEP_TIME = 1000;
    private static final int MAX_RETRIES = 3;


    public static void main(String[] args) {
        // Retry strategy. Retry 3 times, and will increase the sleep time between retries.
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_TIME, MAX_RETRIES);
        CuratorFramework zkClient = CuratorFrameworkFactory.builder()
                // the server to connect to (can be a server list)
                .connectString("119.3.57.241:2181")
                .retryPolicy(retryPolicy)
                .build();
        zkClient.start();

        try {
//            zkClient.create().forPath("/node1");
            zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/node1/00001","java".getBytes());
            byte[] bytes = zkClient.getData().forPath("/node1/00001");//获取节点的数据内容，获取到的是 byte数组
            System.out.println(bytes.toString() + "--------------------------------------");
            System.out.println(zkClient.checkExists().forPath("/node1/00001"));//不为null的话，说明节点创建成功);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
